package com.mobilekites.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Response.ErrorListener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.mobilekites.R;
import com.mobilekites.api.DfeApi;
import com.mobilekites.api.model.OnDataChangedListener;
import com.mobilekites.layout.ContentFrame;
import com.mobilekites.layout.LayoutSwitcher;
import com.mobilekites.layout.LayoutSwitcher.RetryButtonListener;
import com.mobilekites.util.ErrorStrings;

public abstract class PageFragment extends Fragment implements ErrorListener,
		OnDataChangedListener, RetryButtonListener {
	protected Context mContext;
	private static final String KEY_THEME = "kites.PageFragment.theme";

	protected ViewGroup mDataView;
	protected DfeApi mDfeApi;
	private LayoutSwitcher mLayoutSwitcher;
	protected PageFragmentHost mPageFragmentHost;
	protected boolean mRefreshRequired;
	protected boolean mSaveInstanceStateCalled;
	private int mTheme;
	protected ImageLoader mImageLoader;

	public PageFragment() {
		setArguments(new Bundle());
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		if ((PageFragmentHost) getActivity() != mPageFragmentHost) {
			mPageFragmentHost = (PageFragmentHost) getActivity();
			mContext = getActivity();
			mDfeApi = mPageFragmentHost.getDfeApi();
			mImageLoader = mPageFragmentHost.getImageLoader();
			onInitViewBinders();
		}
		mSaveInstanceStateCalled = false;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mSaveInstanceStateCalled = false;
	}

	@Override
	public View onCreateView(LayoutInflater layoutinflater,
			ViewGroup viewgroup, Bundle bundle) {
		if (android.os.Build.VERSION.SDK_INT >= 11) {
			if (bundle != null)
				mTheme = bundle.getInt(KEY_THEME, mTheme);
			layoutinflater = LayoutInflater.from(new ContextThemeWrapper(
					getActivity(), mTheme));
		}
		// ContentFrame contentframe = (ContentFrame) layoutinflater.inflate(
		// R.layout.content_frame_layout, viewgroup, false);
		ContentFrame contentframe = new ContentFrame(getActivity());
		contentframe.setDataLayout(layoutinflater, getLayoutRes(),
				R.id.kites_content_layout);
		mDataView = contentframe.getDataLayout();
		mSaveInstanceStateCalled = false;
		mLayoutSwitcher = new LayoutSwitcher(contentframe,
				R.id.kites_content_layout, R.id.kites_error_layout,
				R.id.kites_loading_layout, this, LayoutSwitcher.DATA_MODE);
		return contentframe;
	}

	protected abstract int getLayoutRes();

	@Override
	public void onRetry() {
		refresh();
	}

	@Override
	public void onDataChanged() {
		if (!isAdded()) {
			mRefreshRequired = true;
		} else {
			mRefreshRequired = false;
			switchToData();
			rebindViews();
		}
	}

	protected abstract void onInitViewBinders();

	public void onResume() {
		super.onResume();
		mSaveInstanceStateCalled = false;
		if (mRefreshRequired)
			refresh();
	}

	public boolean canChangeFragmentManagerState() {
		FragmentActivity fragmentactivity = getActivity();
		return mSaveInstanceStateCalled || fragmentactivity == null ? false
				: true;
	}

	@Override
	public void onErrorResponse(VolleyError volleyerror) {
		if (canChangeFragmentManagerState())
			switchToError(ErrorStrings.get(mContext, volleyerror));
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		mDataView = null;
		mLayoutSwitcher = null;
	}

	protected abstract void rebindViews();

	public void onSaveInstanceState(Bundle bundle) {
		bundle.putInt(KEY_THEME, mTheme);
		mSaveInstanceStateCalled = true;
	}

	public void refresh() {
		mRefreshRequired = false;
		requestData();
	}

	public void refreshOnResume() {
		mRefreshRequired = true;
	}

	protected abstract void requestData();

	protected void setTheme(int id) {
		mTheme = id;
	}

	protected void switchToBlank() {
		mLayoutSwitcher.switchToBlankMode();
	}

	protected void switchToData() {
		mLayoutSwitcher.switchToDataMode();
	}

	protected void switchToError(String msg) {
		if (mLayoutSwitcher != null) {
			mLayoutSwitcher.switchToErrorMode(msg);
		}
	}

	protected void switchToLoading() {
		mLayoutSwitcher.switchToLoadingDelayed(350);
	}

}
